﻿using System.Data;
using NetSurveillance.WebApp.DataServices.Implementations;
using NetSurveillance.WebApp.DataServices.Models;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.PostgreSQL;

namespace NetSurveillance.WebApp.DataServices
{
    public sealed class DataProvider
    {
        #region DataServices
        private readonly object _syncObj = new object();
        private readonly string _defaultConnection;

        private UserService _userService;
        private CaseService _caseService;
        private SearchService _searchService;
        private ProtocolService _protocolsService;
        private IMDataSearchService _imDataSearchService;
        private MonitorSettingService _monitorSettingService;
        private HostService _hostService;
        private DisplayFieldService _displayFieldService;
        private AlertSettingService _alertSettingService;
        private IpSettingService _ipSettingService;
        private SetupWizardService _setupWizardService;
        private QQPasswordService _qqPasswordService;
        private HostScanResultService _hostScanResultService;
        private TaskService _taskService;
        private TaskSettingService _taskSettingService;
        private KeywordService _keywordService;
        private InteractiveService _interactiveService;
        private QQMonitorHostService _qqMonitorHostService;
        private DataRecordService _dataRecordService;
        private SensitiveAccountService _sensitiveAccountService;
        private WhiteListService _whiteListService;
        private ServerStatusService _serverStatusService;
        private DataIMRecordService _dataIMRecordService;
        private VirusEmbedsService _virusEmbedsService;
        private RemoteServerService _remoteServerService;

        #endregion
        public DataProvider(string connectionStr)
        {
            _defaultConnection = connectionStr;
        }

        public void InitDataBase(bool isOverwrite = false)
        {
            IOrmLiteDialectProvider provider = PostgreSQLDialectProvider.Instance;
            OrmLiteConfig.DialectProvider = provider;
            var dbFactory = new OrmLiteConnectionFactory(_defaultConnection, provider);
            using (IDbConnection db = dbFactory.OpenDbConnection())
            {
                //移除表的顺序和添加表的顺序必须要符合依赖关系，否则会失败，因为ServiceStack这个库移除Postgresql数据库时
                //不会加上casecde,先删除从表，再删除主表
                if (isOverwrite)
                {
                    db.DropTable<UserPermission>();
                    db.DropTable<RemoteServer>();
                    db.DropTable<AccountAlias>();
                    db.DropTable<WhiteListHost>();
                    db.DropTable<WhiteList>();
                    db.DropTable<DisplayField>();
                    db.DropTable<ServerStatus>();
                    db.DropTable<DataRecordKeyword>();
                    db.DropTable<DataRecordAction>();
                    db.DropTable<DataRecord>();
                    db.DropTable<DataIMRecord>();
                    db.DropTable<SensitiveAccount>();
                    db.DropTable<QQMonitorHost>();
                    db.DropTable<VirusEmbedHost>();
                    db.DropTable<TaskVirusEmbedRule>();
                    db.DropTable<VirusEmbedRule>();
                    db.DropTable<IpSetting>();
                    db.DropTable<Interactive>();
                    db.DropTable<TaskSetting>();
                    db.DropTable<HostScanResult>();
                    db.DropTable<AlertSetting>();
                    db.DropTable<KeywordTemplate>();
                    db.DropTable<Keyword>();
                    db.DropTable<SetupWizard>();
                    db.DropTable<CasePermission>();
                    db.DropTable<Protocol>();
                    db.DropTable<MultiSearch>();
                    db.DropTable<IMDataSearch>();
                    db.DropTable<MonitorSetting>();
                    db.DropTable<Host>();
                    db.DropTable<QQPassword>();
                    db.DropTable<Task>();
                    db.DropTable<Case>();
                    db.DropTable<User>();
                
                }
                db.CreateTable<User>(isOverwrite);
                db.CreateTable<Case>(isOverwrite);
                db.CreateTable<Task>(isOverwrite);
                db.CreateTable<CasePermission>(isOverwrite);
                db.CreateTable<Protocol>(isOverwrite);
                db.CreateTable<MultiSearch>(isOverwrite);
                db.CreateTable<IMDataSearch>(isOverwrite);
                db.CreateTable<MonitorSetting>(isOverwrite);
                db.CreateTable<Host>(isOverwrite);
                db.CreateTable<DisplayField>(isOverwrite);
                db.CreateTable<IpSetting>(isOverwrite);
                db.CreateTable<KeywordTemplate>(isOverwrite);
                db.CreateTable<Keyword>(isOverwrite);
                db.CreateTable<SetupWizard>(isOverwrite);
                db.CreateTable<AlertSetting>(isOverwrite);
                db.CreateTable<HostScanResult>(isOverwrite);
                db.CreateTable<TaskSetting>(isOverwrite);
                db.CreateTable<Interactive>(isOverwrite);
                db.CreateTable<VirusEmbedRule>(isOverwrite);
                db.CreateTable<TaskVirusEmbedRule>(isOverwrite);
                db.CreateTable<VirusEmbedHost>(isOverwrite);
                db.CreateTable<QQMonitorHost>(isOverwrite);
                db.CreateTable<QQPassword>(isOverwrite);
                db.CreateTable<SensitiveAccount>(isOverwrite);
                db.CreateTable<DataIMRecord>(isOverwrite);
                db.CreateTable<DataRecord>(isOverwrite);
                db.CreateTable<DataRecordAction>(isOverwrite);
                db.CreateTable<DataRecordKeyword>(isOverwrite);
                db.CreateTable<ServerStatus>(isOverwrite);
                db.CreateTable<WhiteList>(isOverwrite);
                db.CreateTable<WhiteListHost>(isOverwrite);
                db.CreateTable<AccountAlias>(isOverwrite);
                db.CreateTable<RemoteServer>(isOverwrite);
                db.CreateTable<UserPermission>(isOverwrite);
                DataInit.InitData(this);
                DataInit.InitFunction(db);
            }
        }
        #region ModelService
        public UserService UserService
        {
            get
            {
                if (_userService == null)
                    lock (_syncObj)
                    {
                        _userService = new UserService(_defaultConnection);
                    }
                return _userService;
            }
        }

        public CaseService CaseService
        {
            get
            {
                if (_caseService == null)
                    lock (_syncObj)
                    {
                        _caseService = new CaseService(_defaultConnection);
                    }
                return _caseService;
            }
        }

        public SearchService SearchService
        {
            get
            {
                if (_searchService == null)
                    lock (_syncObj)
                    {
                        _searchService = new SearchService(_defaultConnection);
                    }
                return _searchService;
            }
        }

        public ProtocolService ProtocolsService
        {
            get
            {
                if (_protocolsService == null)
                    lock (_syncObj)
                    {
                        _protocolsService = new ProtocolService(_defaultConnection);
                    }
                return _protocolsService;
            }
        }

        public IMDataSearchService IMDataSearchService
        {
            get
            {
                if (_imDataSearchService == null)
                    lock (_syncObj)
                    {
                        _imDataSearchService = new IMDataSearchService(_defaultConnection);
                    }
                return _imDataSearchService;
            }
        }

        public MonitorSettingService MonitorSettingService
        {
            get
            {
                if (_monitorSettingService == null)
                    lock (_syncObj)
                    {
                        _monitorSettingService = new MonitorSettingService(_defaultConnection);
                    }
                return _monitorSettingService;
            }
        }

        public HostService HostService
        {
            get
            {
                if (_hostService == null)
                    lock (_syncObj)
                    {
                        _hostService = new HostService(_defaultConnection);
                    }
                return _hostService;
            }
        }

        public DisplayFieldService DisplayFieldService
        {
            get
            {
                if (_displayFieldService == null)
                    lock (_syncObj)
                    {
                        _displayFieldService = new DisplayFieldService(_defaultConnection);
                    }
                return _displayFieldService;
            }
        }

        public AlertSettingService AlertSettingService
        {
            get
            {
                if (_alertSettingService == null)
                    lock (_syncObj)
                    {
                        _alertSettingService = new AlertSettingService(_defaultConnection);
                    }
                return _alertSettingService;
            }
        }

        public IpSettingService IpSettingService
        {
            get
            {
                if(_ipSettingService == null)
                    lock (_syncObj)
                    {
                        _ipSettingService = new IpSettingService(_defaultConnection);
                    }
                return _ipSettingService;
            }
        }

        public SetupWizardService SetupWizardService
        {
            get
            {
                if(_setupWizardService == null)
                    lock(_syncObj)
                    {
                        _setupWizardService = new SetupWizardService(_defaultConnection);
                    }
                return _setupWizardService;
            }
        }

        public KeywordService KeywordService
        {
            get
            {
                if(_keywordService == null)
                    lock (_syncObj)
                    {
                        _keywordService = new KeywordService(_defaultConnection);
                    }
                return _keywordService;
            }
        }

        public QQPasswordService QQPasswordService
        {
            get
            {
                if (_qqPasswordService == null)
                    lock (_syncObj)
                    {
                        _qqPasswordService = new QQPasswordService(_defaultConnection);
                    }
                return _qqPasswordService;
            }
        }

        public HostScanResultService HostScanResultService
        {
            get
            {
                if (_hostScanResultService == null)
                    lock (_syncObj)
                    {
                        _hostScanResultService = new HostScanResultService(_defaultConnection);
                    }
                return _hostScanResultService;
            }
        }

        public TaskSettingService TaskSettingService
        {
            get
            {
                if (_taskSettingService == null)
                    lock (_syncObj)
                    {
                        _taskSettingService = new TaskSettingService(_defaultConnection);
                    }
                return _taskSettingService;
            }
        }

        public InteractiveService InteractiveService
        {
            get
            {
                if (_interactiveService == null)
                    lock (_syncObj)
                    {
                        _interactiveService = new InteractiveService(_defaultConnection);
                    }
                return _interactiveService;
            }
        }

        public TaskService TaskService
        {
            get
            {
                if (_taskService == null)
                    lock (_syncObj)
                    {
                        _taskService = new TaskService(_defaultConnection);
                    }
                return _taskService;
            }
        }

        public QQMonitorHostService QQMonitorHostService
        {
            get
            {
                if (_qqMonitorHostService == null)
                    lock (_syncObj)
                    {
                        _qqMonitorHostService = new QQMonitorHostService(_defaultConnection);
                    }
                return _qqMonitorHostService;
            }
        }

        public DataRecordService DataRecordService
        {
            get
            {
                if(_dataRecordService == null)
                    lock (_syncObj)
                    {
                        _dataRecordService = new DataRecordService(_defaultConnection);
                    }
                return _dataRecordService;
            }
        }

        public SensitiveAccountService SensitiveAccountService
        {
            get
            {
                if(_sensitiveAccountService == null)
                    lock (_syncObj)
                    {
                        _sensitiveAccountService = new SensitiveAccountService(_defaultConnection);
                    }
                return _sensitiveAccountService;
            }
        }

        public WhiteListService WhiteListService
        {
            get
            {
                if (_whiteListService == null)
                    lock (_syncObj)
                    {
                        _whiteListService = new WhiteListService(_defaultConnection);
                    }
                return _whiteListService;
            }
        }

        public ServerStatusService ServerStatusService
        {
            get
            {
                if (_serverStatusService == null)
                    lock (_syncObj)
                    {
                        _serverStatusService = new ServerStatusService(_defaultConnection);
                    }
                return _serverStatusService;
            }
        }

        public DataIMRecordService DataIMRecordService
        {
            get
            {
                if (_dataIMRecordService == null)
                    lock (_syncObj)
                    {
                        _dataIMRecordService = new DataIMRecordService(_defaultConnection);
                    }
                return _dataIMRecordService;
            }
        }

        public VirusEmbedsService VirusEmbedsService
        {
            get
            {
                if(_virusEmbedsService == null)
                    lock (_syncObj)
                    {
                        _virusEmbedsService = new VirusEmbedsService(_defaultConnection);
                    }
                return _virusEmbedsService;
            }
        }

        public RemoteServerService RemoteServerService
        {
            get
            {
                if(_remoteServerService == null)
                    lock (_syncObj)
                    {
                        _remoteServerService = new RemoteServerService(_defaultConnection);
                    }
                return _remoteServerService;
            }
        }
        #endregion
    }
}